package com.atguigu.edupublisher.controller;

import com.atguigu.edupublisher.beans.TrafficUvCt;
import com.atguigu.edupublisher.service.TrafficStatsService;
import com.atguigu.edupublisher.utils.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TrafficStatsController
 * @Description TODO
 * @Author$ 邢家俊
 * @Date 2023-5-8 20:22
 * @Version 1.0
 **/
@RestController
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;


    @RequestMapping("/ch")
    public String getChUvCt(@RequestParam(value = "date",defaultValue = "0") Integer date , @RequestParam(value = "limit",defaultValue = "10")Integer limit){
        if(date ==0){
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> chUvCt = trafficStatsService.getChUvCt(date, limit);
        StringBuilder stringBuilder = new StringBuilder();
        ArrayList<String> chList = new ArrayList<>();
        ArrayList<Integer> uvCtList = new ArrayList<>();

        for (int i = 0; i < chUvCt.size(); i++) {
            TrafficUvCt trafficUvCt = chUvCt.get(i);
            uvCtList.add(trafficUvCt.getUvCt());
            chList.add(trafficUvCt.getCh());
        }
        String chUvCtStr = "{\"status\": 0,\"data\": {\"categories\": [\""+ StringUtils.join(chList,"\",\"")+
                "\" ],\"series\": [{\"name\": \"渠道\",  \"data\": [ "+StringUtils.join(uvCtList,",")+" ]}]}}";
        return chUvCtStr;

///,"series": [{"name": "手机品牌",  "data": [

    }

}